package card_discount

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// SetCardDiscountItemCategoriesParam 设置卡折扣商品类目配置
type SetCardDiscountItemCategoriesParam struct {
	CardDiscountPtr      *sqlc.CardDiscount
	ItemCategoryConfPtrs param.ItemCategoryConfigurationPtrArray
}

// SetCardDiscountItemCategories 设置卡折扣商品类目配置
func (s *CardDiscountEntityServiceImp) SetCardDiscountItemCategories(ctx context.Context, q *sqlc.Queries, ptr *SetCardDiscountItemCategoriesParam) (err error) {
	cardDiscountID := ptr.CardDiscountPtr.ID
	err = q.DisableCardDiscountItemCategoriesByCardDiscountID(ctx, cardDiscountID)
	if err != nil {
		return
	}
	if len(ptr.ItemCategoryConfPtrs) > 0 {
		for idx, itemCategoryConf := range ptr.ItemCategoryConfPtrs {
			err = q.UpsertCardDiscountItemCategory(ctx, sqlc.UpsertCardDiscountItemCategoryParams{
				CardDiscountID:   cardDiscountID,
				OutCardID:        ptr.CardDiscountPtr.OutCardID,
				ItemCategoryID:   itemCategoryConf.ID,
				ItemCategoryName: itemCategoryConf.Name,
				SortNo:           int32(idx + 1),
			})
			if err != nil {
				return
			}
		}
	}
	return
}
